package com.easy.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.easy.auth.domain.Report;

public abstract class FileUtil {
	/**
	 * 复制单个文件
	 * 
	 * @param oldPath
	 *            String 原文件路径 如：c:/fqf.txt
	 * @param newPath
	 *            String 复制后路径 如：f:/fqf.txt
	 * @return boolean
	 */
	public static void copyFile(String oldPath, String newPath) {
		try {
			int bytesum = 0;
			int byteread = 0;
			File oldfile = new File(oldPath);
			if (oldfile.exists()) { // 文件存在时
				InputStream inStream = new FileInputStream(oldPath); // 读入原文件
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				int length;
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; // 字节数 文件大小
					System.out.println(bytesum);
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
			}
		} catch (Exception e) {
			System.out.println("复制文件操作出错"+oldPath);
			e.printStackTrace();

		}

	}

	public static void copyfileChannel(String oldPath, String newPath) {

		FileInputStream fi = null;
		FileOutputStream fo = null;
		FileChannel in = null;
		FileChannel out = null;
		File oldfile = new File(oldPath);
		File newfile = new File(newPath);
		if (newfile.exists()) {
			//System.out.println(oldPath+"文件已存在"+newPath);
			return;
		}
		try {
			if (oldfile.exists()) { // 文件存在时
				fi = new FileInputStream(oldfile);
				fo = new FileOutputStream(newfile);
				in = fi.getChannel();// 得到对应的文件通道
				out = fo.getChannel();// 得到对应的文件通道
				in.transferTo(0, in.size(), out);// 连接两个通道，并且从in通道读取，然后写入out通道
			}else{
				System.err.println("复制文件操作出错"+oldPath+"不存在");
				return ;
				}
		} catch (IOException e) {
			System.err.println("复制文件操作出错"+oldPath);
			e.printStackTrace();
		} finally {
			try {
				fi.close();
				in.close();
				fo.close();
				out.close();
			} catch (IOException e) {
				System.err.println("关闭通道出错");
				
			}
		}
	}
	/**
	 * 复制整个文件夹内容
	 * 
	 * @param oldPath
	 *            String 原文件路径 如：c:/fqf
	 * @param newPath
	 *            String 复制后路径 如：f:/fqf/ff
	 * @return boolean
	 */
	public static void copyFolder(String oldPath, String newPath) {

		try {
			(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
			File a = new File(oldPath);
			String[] file = a.list();
			File temp = null;
			for (int i = 0; i < file.length; i++) {
				if (oldPath.endsWith(File.separator)) {
					temp = new File(oldPath + file[i]);
				} else {
					temp = new File(oldPath + File.separator + file[i]);
				}

				if (temp.isFile()) {
					FileInputStream input = new FileInputStream(temp);
					FileOutputStream output = new FileOutputStream(newPath
							+ "/" + (temp.getName()).toString());
					byte[] b = new byte[1024 * 5];
					int len;
					while ((len = input.read(b)) != -1) {
						output.write(b, 0, len);
					}
					output.flush();
					output.close();
					input.close();
				}
				if (temp.isDirectory()) {// 如果是子文件夹
					copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
				}
			}
		} catch (Exception e) {
			System.out.println("复制整个文件夹内容操作出错");
			e.printStackTrace();

		}

	}
	public static void loginNAS(){ 
		String host=Parameter.NAS_REPORT_ROOT;
		String user=Parameter.NAS_USER;
		String pass=Parameter.NAS_PASS;
	    String info = "net use " + host + " " + pass + " /user:" + user;  
	    try {  
	        Process process = Runtime.getRuntime().exec(info);  
	        InputStream is =  process.getInputStream();  
	        BufferedReader buf = new BufferedReader(new InputStreamReader(is, "gbk"));  
	        String data = null;  
	        while ((data = buf.readLine()) != null){  
	            System.out.println(data+"---loginNAS");  
	        }  
	        buf.close();  
//	        File a= new File(host);
//	        for (int i = 0; i < a.listFiles().length; i++) {
//				System.out.println(a.listFiles()[i].getName());
//			}
	        		
	    } catch (IOException e) {  
	        System.out.println("login failed: " + e.getMessage());  
	    }  
	}  
	
	public static void loginDPVAPP1(){ 
		String host=Parameter.DPVAPP1_REPORT_ROOT;
		String user=Parameter.DPVAPP1_USER;
		String pass=Parameter.DPVAPP1_PASS;
	    String info = "net use " + host + " " + pass + " /user:" + user;  
	    try {  
	        Process process = Runtime.getRuntime().exec(info);  
	        InputStream is =  process.getInputStream();  
	        BufferedReader buf = new BufferedReader(new InputStreamReader(is, "gbk"));  
	        String data = null;  
	        while ((data = buf.readLine()) != null){  
	            System.out.println(data+"---loginDPVAPP1");  
	        }  
	        buf.close();  
	    } catch (IOException e) {  
	        System.out.println("login failed: " + e.getMessage());  
	    }  
	}  
	
	
	static Hashtable<String, String> MIME;
	static {
	MIME = new Hashtable<String, String>();
	MIME.put("jpeg", "image/jpeg");
	MIME.put("jpg", "image/jpeg");
	MIME.put("jfif", "image/jpeg");
	MIME.put("jfif-tbnl", "image/jpeg");
	MIME.put("jpe", "image/jpeg");
	MIME.put("jfif", "image/jpeg");
	MIME.put("tiff", "image/tiff");
	MIME.put("tif", "image/tiff");
	MIME.put("gif", "image/gif");
	MIME.put("xls", "application/x-msexcel");
	MIME.put("doc", "application/msword");
	MIME.put("ppt", "application/x-mspowerpoint");
	MIME.put("zip", "application/x-zip-compressed");
	MIME.put("pdf", "application/pdf");
}

/**
 * 对字节流进行处理，图片显示，其他提供下载
 * @param fileName 文件名称
 * @param bytes[] 文件二进制流
 * @param down 是否下载
 * 
 * @return
 */
public static void StreamOper(HttpServletResponse response, String fileName, String fileURL, boolean down)
	throws IOException {
	
	byte[]  bytes = readerFileStream(fileURL);
		int index = 0;
		String ext = "";
		if ((index = fileName.indexOf('.')) > 0)
			ext = fileName.substring(index + 1);
		//通过文件名的后缀判断文件的格式
		String mime = (String) MIME.get(ext);
		if (mime == null)
			mime = "application/x-msdownload";

                    response.setContentType(mime);
		//是否需要提供下载
		if (down)
			response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
		OutputStream outStream = response.getOutputStream();
		outStream.write(bytes, 0, bytes.length);
		outStream.flush();
		outStream.close();
	}


public static byte[] readerFileStream(String fileName)  
        throws IOException {  
    File f = new File(fileName);  
    int length = (int)f.length();  
      
    byte[] buff = new byte[length];  
      
    BufferedInputStream bis = null;  
      
  
    bis = new BufferedInputStream(new FileInputStream(fileName));  
    int bytesRead;  
    while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
    }  
      
    return buff;  
}  
}
